<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Hash;
use Auth;
use Validator;

class AdminPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
//      如果你的登录验证是使用中间件来执行的，那么这里就返回true
        return true;
    }

//    添加验证规则
    public function addValidator()
    {
//        验证用户密码
        Validator::extend('check_password',function ($attribute,$value,$parameters,$validator){
           return Hash::check($value,Auth::guard('admin')->user()->password);
        });
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $this->addValidator();

        return [
            //设置规则，即哪些字段必须验证(required)
            'old_password' => 'sometimes|required|check_password',
            'password' => 'sometimes|required|confirmed',
            'password_confirmation' => 'sometimes|required',
        ];
    }

    public function messages()
    {
        return [
            'old_password.required' => '原密码输入错误',
            'old_password.check_password' => '原密码输入错误',
            'password.required' => '密码不能为空',
            'password_confirmation.required'=>'确认密码不能为空',
            'password.confirmed' => '两次密码输入不一致',
        ];
    }
}
